package ds.adt;

import java.util.Iterator;

/**
 * Implements Bag data structure.
 * 
 * @author shankar
 * 
 * @param <Item>
 */
public class Bag<Item> implements Iterable<Item> {

	private Node first;

	private class Node {
		private Item data;
		private Node next;
	}

	public void add(Item item) {
		Node current = first;
		first = new Node();
		first.data = item;
		first.next = current;
	}

	@Override
	public Iterator<Item> iterator() {
		return new ListIterator();
	}

	private class ListIterator implements Iterator<Item> {

		private Node current = first;

		@Override
		public boolean hasNext() {
			return (current != null);
		}

		@Override
		public Item next() {
			Item item = current.data;
			current = current.next;
			return item;
		}

		@Override
		public void remove() {
		}
	}
}
